package com.hc.tenant.config;

import com.hc.tenant.listener.MqttEvenPublisher;
import com.hc.tenant.listener.event.MqttEvent;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

@Configuration
@ConfigurationProperties(prefix = "mqtt")

public class MqttConfig {
    private String url;
    private String clientId;
    private String[] topicList;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String[] getTopicList() {
        return topicList;
    }

    public void setTopicList(String[] topicList) {
        this.topicList = topicList;
    }

    @Autowired
    MqttCallback mqttCallback;

    @Autowired
    private MqttEvenPublisher evenPublisher;


    @Bean
    public MqttClient client() throws MqttException {
        MqttClient mqttClient = null;
        mqttClient = new MqttClient(url, clientId, new MemoryPersistence());
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(false);
        options.setAutomaticReconnect(true);
        mqttClient.connect(options);
        mqttClient.setCallback(mqttCallback);
        return mqttClient;

    }

    @Bean
    @RefreshScope
    public MqttPushClient mqttPushClient(MqttClient client){
        MqttPushClient mqttPushClient = new MqttPushClient(client);
        for (int i = 0; i < topicList.length; i++) {
            mqttPushClient.subscribe(topicList[i]);
        }
        return mqttPushClient;
    }



        @Component
        public class MqttDefaultCallBack implements MqttCallbackExtended {

            @Autowired
            private MqttConfig mqttConfig;

            @Autowired
            private MqttPushClient mqttPushClient;


            @Override
            public void connectionLost(Throwable cause) {

            }

            @Override
            public void messageArrived(String topic, MqttMessage message) {
                String[] split = topic.split("/");
                String serviceName = split[1];
                String token = split[2];
                evenPublisher.mqttEventPublish(new MqttEvent(serviceName,message, token));
            }
            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {
            }

            @Override
            public void connectComplete(boolean reconnect, String serverURI) {
                String[] topicList = mqttConfig.getTopicList();
                for (int i = 0; i < topicList.length; i++) {
                    mqttPushClient.subscribe(topicList[i]);
                }
            }
        }
    }